﻿using ChestPainCenterSystem.Areas.ChestPainCenter.Models;
using ChestPainCenterSystem.Code.Bll;
using ChestPainCenterSystem.Core;
using ChestPainCenterSystem.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ChestPainCenterSystem.Areas.ChestPainCenter.Controllers
{
    public class DisRecordController : BaseController
    {
        //诊疗记录相关
        // GET: /ChestPainCenter/DisRecord/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Edit(int id = 0)
        {
            zhenliaojilu zhenliaojilu = new zhenliaojilu();
            if (id > 0)
            {
                OperateResult resultInfo = ZhenLiaoJiLu.GezhenliaojiluList(1, 1, "", id);
                if (resultInfo.Result == 1)
                {
                    var d = JsonConvert.DeserializeObject<dynamic>(resultInfo.Body.ToString());
                    zhenliaojilu = JsonConvert.DeserializeObject<zhenliaojilu>(JsonConvert.SerializeObject(d.Rows[0]));   
                }
            }
            ViewBag.Zhenliaojilu = zhenliaojilu;
            return View();
        }

        public ContentResult Save()
        {
            zhenliaojilu zhenliaojilu = new Models.zhenliaojilu();
            zhenliaojilu.code = Request["txtCode"];
            zhenliaojilu.create_time = string.IsNullOrEmpty(WebUtils.GetString("txtTime")) ? null : (DateTime?)Convert.ToDateTime(WebUtils.GetString("txtTime"));
            zhenliaojilu.create_user = Request["txtUser"];
            zhenliaojilu.Id = Convert.ToInt32(Request["txtId"]);
            zhenliaojilu.IsDelete = 0;
            zhenliaojilu.name = Request["txtName"];
            zhenliaojilu.zl_desc = Request["txtDesc"];
            string objstr = JsonHelper.JsonDate(zhenliaojilu);
            OperateResult resultInfo = ZhenLiaoJiLu.Setzhenliaojilu(objstr);
            return JsonDate(resultInfo);
        }

        public ContentResult Search()
        {
            string pageIndex = Request["page"];
            string pageSize = Request["pagesize"];
            string name = Request["txtName"];
            OperateResult resultInfo = ZhenLiaoJiLu.GezhenliaojiluList(Convert.ToInt32(pageIndex), Convert.ToInt32(pageSize), name);
            string NoDate = "{Rows:[],Total:0}";
            if (resultInfo.Result == 1) 
            {
                var listData = JsonConvert.DeserializeObject<dynamic>(resultInfo.Body.ToString());                
                return JsonDate(listData);
            }
            return Content(NoDate);
        }

      
        public ActionResult Delete(int id)
        {
            OperateResult resultInfo = ZhenLiaoJiLu.Delete(id);
            return JsonDate(resultInfo);
        }
    }
}
